package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service(interfaceClass = UserService.class)
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByUsername(String username) {
        return userMapper.findByUsername(username);
    }

    /**
     * 新增检查组
     * @param checkItemIds
     * @param
     */
    @Override
    public void add(Integer[] checkItemIds, User user) {

        //先新增检查组
        userMapper.add(user);

        //添加检查组和检查项的中间表关系
        setCheckGroupAndCheckItem(checkItemIds,user.getId());

    }

    private void setCheckGroupAndCheckItem(Integer[] checkItemIds, Integer id) {

        if (checkItemIds!=null && checkItemIds.length>0){
            for (Integer checkItemId : checkItemIds) {

                userMapper.setUserAndRole(checkItemId,id);
            }
        }

    }
    //添加检查组和检查项的中间表关系

    /*private void setCheckGroupAndCheckItem(Integer[] checkItemIds, Integer id) {

        if (checkItemIds!=null && checkItemIds.length>0){
            for (Integer checkItemId : checkItemIds) {
                checkGroupMapper.setCheckGroupAndCheckItem(checkItemId,id);
            }
        }

    }*/

    /**
     * 查询检查组的分页数据
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    @Override
    public PageResult findPage(Integer currentPage,Integer pageSize,String queryString) {

        PageHelper.startPage(currentPage,pageSize);

        //条件查询数据
        List<User> checkGroupList = userMapper.findByCondition(queryString);

        //数据包装
        PageInfo<User> pageInfo = new PageInfo<>(checkGroupList);

        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 通过id查到检查组
     */
    @Override
    public User findById(Integer id) {
        return userMapper.findById(id);
    }

    /**
     * 编辑更新
     * @param checkItemIds
     * @param
     */
    @Override
    public void edit(Integer[] checkItemIds, User user) {

        //删除中间表关系
        userMapper.deleteAssociation(user.getId());

        //重新设置中间表关系
        setCheckGroupAndCheckItem(checkItemIds,user.getId());

        //更新检查组
        userMapper.edit(user);


    }

    /**
     * 通过id删除检查组
     * @param id
     */
    @Override
    public void deleteById(Integer id) {


        //判断检查组有没有被套餐引用
        Integer count = userMapper.findCountById(id);
        if (count>0){
            throw new RuntimeException(MessageConstant.CHECKGROUP_IS_QUOTED);
        }

        //删除检查组和检查项的中间表关系
        userMapper.deleteAssociation(id);

        //删除检查组
        userMapper.deleteById(id);


    }

    /**
     * 查询所有检查组
     */
    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    @Override
//查询当前用户名是否重复
    public Boolean checkUsername(String username) {
        Boolean flag = userMapper.checkUsername(username) == null;  //false  admin已经存在了所以不为空
        return flag;
    }
}
